home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Memory / Queues / FiniteQueueBase.cp < prev    next >
Encoding:
Text File  |  1997-06-28  |  1.3 KB  |  61 lines  |  [TEXT/CWIE]

  1. // FiniteQueueBase.cp
  2.  
  3. #ifndef FiniteQueueBase_h
  4. #include "FiniteQueueBase.h"
  5. #endif
  6. #ifndef Assert_h
  7. #include "Assert.h"
  8. #endif
  9.  
  10. FiniteQueueBase::FiniteQueueBase( void *theStart,
  11.                                              uint32 theLength,
  12.                                              uint32 theElementSize )
  13.   : start( static_cast<uint8 *>(theStart) ),
  14.      end( static_cast<uint8 *>(theStart) + theLength * theElementSize ),
  15.      elementSize( theElementSize ),
  16.      length( theLength ),
  17.      allocated( 0 ),
  18.      allocateNext( static_cast<uint8 *>(theStart) ),
  19.      disposeNext( static_cast<uint8 *>(theStart) )
  20.   {
  21.     Assert( CanMultiply( theLength, theElementSize ) )
  22.   }
  23.  
  24. void *FiniteQueueBase::Allocate( uint32 size )
  25.   {
  26.     Assert( size == elementSize );
  27.     Assert( !Full() );
  28.     
  29.     Assert( allocateNext >= start );
  30.     Assert( allocateNext < end );
  31.     Assert( ( allocateNext - start ) % elementSize == 0 );
  32.  
  33.     void *result = allocateNext;
  34.     
  35.     allocateNext += elementSize;
  36.     Assert( allocateNext <= end );
  37.     if ( allocateNext >= end )
  38.         allocateNext = start;
  39.     
  40.     allocated++;
  41.     
  42.     return result;
  43.   }
  44.  
  45. void FiniteQueueBase::Release( void *p )
  46.   {
  47.     Assert( !IsEmpty() );
  48.     Assert( p == disposeNext );
  49.     
  50.     Assert( disposeNext >= start );
  51.     Assert( disposeNext < end );
  52.     Assert( ( disposeNext - start ) % elementSize == 0 );
  53.  
  54.     disposeNext += elementSize;
  55.     Assert( disposeNext <= end );
  56.     if ( disposeNext >= end )
  57.         disposeNext = start;
  58.  
  59.     allocated--;
  60.   }
  61.